package com.phospherus.user.controller

import com.phospherus.common.controller.BaseController
import com.phospherus.common.controller.handler.ResultHandler
import com.phospherus.user.entity.Role
import com.phospherus.user.entity.User
import com.phospherus.user.service.RoleService
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import javax.servlet.http.HttpServletRequest

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/11/30
 */
@Controller
@RequestMapping("/user/role")
class RoleController : BaseController<Role, RoleService>() {
    @RequestMapping("/bindUsers", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun bindUsers(@RequestParam map: Map<String, Any>, request: HttpServletRequest): ResultHandler<Any?> {
        var roleId: Long = (map["roleId"] as String).toLong()
        var roleName: String = map["roleName"] as String
        var userIds = map["userIds"] as String?
        service.bindUsers(roleId, roleName, userIds)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }

    @RequestMapping("/getBindings", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun getBindings(roleId: Long): ResultHandler<User> {
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, service.getBindings(roleId))
    }

    @RequestMapping("/getUnBindings", method = [RequestMethod.GET, RequestMethod.POST])
    @ResponseBody
    fun getUnBindings(roleId: Long): ResultHandler<User> {
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, service.getUnBindings(roleId))
    }
}